package com.android.library.utils.audioRecord;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理录音文件的类
 */
public class FileUtil {

    //private static String rootPath = "hyPhotoRecord";
    //原始文件(不能播放)
    private final static String AUDIO_PCM_BASEPATH = "/pcm/";
    //可播放的高质量音频文件
    public final static String AUDIO_WAV_BASEPATH =  "/wav/";

    public static String getPcmFileAbsolutePath(Context context, String fileName) {
        if (TextUtils.isEmpty(fileName)) {
            throw new NullPointerException("fileName isEmpty");
        }
        if (!isSdcardExit()) {
            throw new IllegalStateException("sd card no found");
        }
        String mAudioRawPath = "";
        if (isSdcardExit()) {
            if (!fileName.endsWith(".pcm")) {
                fileName = fileName + ".pcm";
            }
            //String fileBasePath = Environment.getDataDirectory() + AUDIO_PCM_BASEPATH;
            File file = new File(context.getExternalFilesDir("").getAbsolutePath(), AUDIO_PCM_BASEPATH);
            //创建目录
            if (!file.exists()) {
                file.mkdirs();
            }
            mAudioRawPath = file.getPath() + "/"+  fileName;
        }

        return mAudioRawPath;
    }

    public static String getWavFileAbsolutePath(Context context, String fileName) {
        if (fileName == null) {
            throw new NullPointerException("fileName can't be null");
        }
        if (!isSdcardExit()) {
            throw new IllegalStateException("sd card no found");
        }

        String mAudioWavPath = "";
        if (isSdcardExit()) {
            if (!fileName.endsWith(".wav")) {
                fileName = fileName + ".wav";
            }
            File file = new File(context.getExternalFilesDir("").getAbsolutePath(), AUDIO_WAV_BASEPATH);
            //创建目录
            if (!file.exists()) {
                file.mkdirs();
            }
            mAudioWavPath = file.getPath() + "/" + fileName;
        }
        return mAudioWavPath;
    }

    /**
     * 判断是否有外部存储设备sdcard
     *
     * @return true | false
     */
    public static boolean isSdcardExit() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

}
